package com.tywj.buscustomerapp.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.BroastBean;
import com.tywj.buscustomerapp.utils.ToastUtils;
import com.tywj.buscustomerapp.view.myview.TaylorPopupWindow;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class AutoViewPagerViewAdapter extends PagerAdapter{
    private Context mContext;
    private Activity activity;
    private List<BroastBean> mList = new ArrayList<>();

    public AutoViewPagerViewAdapter(Context mContext, List<BroastBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
        this.activity = (Activity) mContext;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    private Queue<View> viewPool = new LinkedList<>(); //View池

    @Override public Object instantiateItem(final ViewGroup container, final int position) {
        final View view;
        final ViewHolder viewHolder;
        //当池子中有存货就复用，否则才inflate
        if(viewPool.size() > 0){
            view = viewPool.poll();
            viewHolder = (ViewHolder) view.getTag();
        }else{
            view = LayoutInflater.from(mContext).inflate(R.layout.broast_layout, container, false);
            viewHolder = new ViewHolder();
            viewHolder.title = view.findViewById(R.id.broast_title);
            viewHolder.type = view.findViewById(R.id.broast_type);
            view.setTag(viewHolder);
        }
        viewHolder.title.setText(mList.get(position).getTitle());
        viewHolder.type.setText(mList.get(position).getType());
        viewHolder.title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TaylorPopupWindow mPopwindow = new TaylorPopupWindow(mContext);
                mPopwindow.show();
            }
        });
        container.addView(view);
        return view;
    }

    @Override public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        //将当前View加入到池子中
        viewPool.offer((View) object);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    //定义一个ViewHolder
    class ViewHolder{
        TextView  type;
        TextView title;
    }

}
